/**
 * @author harry
 * @date 2022/3/5/20:45
 */

package com.serviceOrchestration.atomic.units;

import com.serviceOrchestration.annotation.AtomicService;
import com.serviceOrchestration.atomic.AtomicUnit;
import com.serviceOrchestration.atomic.AtomicUnitContext;
import com.serviceOrchestration.dao.BlackListDao;
import com.serviceOrchestration.factory.AtomicFactory;
import com.serviceOrchestration.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;

@AtomicService(
        value = AtomicFactory.BEAN_PREFIX + 4,
        name = "黑名单购买控制服务",
        description = "使用黑名单限制用户购买。黑名单上的用户禁止购买此产品"
)
public class BlackListUnit extends AtomicUnit {

    private final BlackListDao blackListDao;

    @Autowired
    public BlackListUnit(BlackListDao blackListDao) {
        this.blackListDao = blackListDao;
    }

    @Override
    protected boolean doHandle(AtomicUnitContext context) {
        User user = (User) context.getContext("user");
        int count = blackListDao.selectIfHasRecord(user.getId());
        if(count != 0) {
            context.setError("不允许购买");
            return false;
        }
        return true;
    }
}
